package com.ptswitch.material.model.security;

import java.io.Serializable;
import java.util.Date;

import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.Id;
import javax.persistence.ManyToOne;
import javax.persistence.Table;

import org.apache.commons.lang3.builder.EqualsBuilder;
import org.apache.commons.lang3.builder.HashCodeBuilder;
import org.apache.tapestry5.beaneditor.DataType;
import org.hxzon.tapestry5.util.TapestryEx;

import com.ptswitch.material.model.common.Uzer;

@Entity
@Table(name = "uzer_role")
public class UserRole implements Serializable {

    private static final long serialVersionUID = 1L;

    @ManyToOne(fetch = FetchType.EAGER)
    @Id
    private Role role;

    @ManyToOne(fetch = FetchType.LAZY)
    @Id
    private Uzer user;

    @DataType(TapestryEx.DataType_daytime)
    private Date createTime;

    //=============
    public UserRole() {
        setCreateTime(new Date());
    }

    public UserRole(Uzer user, Role role) {
        this.user = user;
        this.role = role;
        setCreateTime(new Date());
    }

    @Override
    public int hashCode() {
        return new HashCodeBuilder().append(role.getId())//
                .append(user.getId())//
                .toHashCode();
    }

    @Override
    public boolean equals(Object o) {
        if (o == this) {
            return true;
        }
        if (!(o instanceof UserRole)) {
            return false;
        }
        UserRole other = (UserRole) o;
        return new EqualsBuilder().append(this.role.getId(), other.role.getId())//
                .append(this.user.getId(), other.user.getId())//
                .isEquals();
    }

    //================
    public Role getRole() {
        return role;
    }

    public void setRole(Role role) {
        this.role = role;
    }

    public Uzer getUser() {
        return user;
    }

    public void setUser(Uzer user) {
        this.user = user;
    }

    public Date getCreateTime() {
        return createTime;
    }

    public void setCreateTime(Date createTime) {
        this.createTime = createTime;
    }

}
